package agent_store

import (
	"context"
	"github.com/pkg/errors"
	"github.com/spf13/cast"
	"xialou_platform/services/agent/api/internal/svc"
	"xialou_platform/services/agent/api/internal/types"
	"xialou_platform/services/agent/rpc/agent"

	"github.com/zeromicro/go-zero/core/logx"
)

type StoreBindLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewStoreBindLogic(ctx context.Context, svcCtx *svc.ServiceContext) StoreBindLogic {
	return StoreBindLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *StoreBindLogic) StoreBind(req *types.StoreBindReq) error {
	_, err := l.svcCtx.AgentRpc.StoreChangeBind(l.ctx, &agent.StoreChangeBindReq{
		AgentId: cast.ToInt64(req.AgentId),
		StoreId: cast.ToInt64(req.StoreId),
	})
	if err != nil {
		logx.Errorf("StoreChangeBind error: %v", err)
		return errors.New("绑定失败")
	}
	return nil
}
